package com.dailyyoga.view;

import java.util.ArrayList;
import java.util.List;

import com.tools.MultipleScreenTool;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.util.Log;

public class TextPage {

	List<Page> mPagas;
	int mWidth;
	int mHight;
	String[] mBuffer;
	Context mContext;
	float yOffset;
	float xOffset;
	Paint mPaint;
	float top = 0, left = 0, right = 0, buttom = 0;
	int mfontSize;

	public class Page {

		int mBufferIndex = 0;
		int mBufferOffset = 0;

		public Page(int index, int offset) {
			mBufferIndex = index;
			mBufferOffset = offset;
		}

		public void draw(Canvas canvas) {
			float x = left;
			float rowH = getRowH();
			float y = rowH / 2 + top;

			if (mPagas.size() == 1 && mYend != y) {
				y = (rowH / 2 + top) + (getDrawH() - mYend) / 2;
			}

			int bufferIndex = mBufferIndex;
			String content[] = mBuffer[bufferIndex].split(" ");
			int offset = mBufferOffset;

			while (bufferIndex < mBuffer.length) {

				if (mBuffer[bufferIndex].equals("") && y == rowH / 2) {
					bufferIndex++;
					continue;
				}

				if (content == null) {
					content = mBuffer[bufferIndex].split(" ");
					offset = 0;
				}

				float rowWidth = 0;
				List<Float> floats = new ArrayList<Float>();

				boolean flag = false;

				for (int i = offset; i < content.length; i++) {

					float wordsWidth = mPaint.measureText(content[i]);

					rowWidth += wordsWidth;

					if (rowWidth > getDrawW()) {

						rowWidth -= wordsWidth;
						flag = true;
						break;
					}
					rowWidth += xOffset;
					floats.add(wordsWidth);
				}

				float xx = (getDrawW() - rowWidth) / (floats.size() - 1);

				for (Float float1 : floats) {
					canvas.drawText(content[offset], x, y, mPaint);
					x += float1;

					if (flag) {
						x += xx;
					}

					x += xOffset;
					offset++;
				}
				y += rowH;
				x = left;

				if (y >= getDrawH()) {
					break;
				}

				if (offset >= content.length) {
					offset = 0;
					content = null;
					bufferIndex++;
				}

			}

		}

	}

	public class RowPage extends Page {

		public RowPage(int index, int offset) {
			super(index, offset);
		}

		public void draw(Canvas canvas) {

			float rowH = getRowH();
			float y = (rowH / 2 + top);

			if (mPagas.size() == 1 && mYend != y) {
				y = (rowH / 2 + top) + (getDrawH() - mYend) / 2;
			}

			int bufferIndex = mBufferIndex;
			String content = mBuffer[bufferIndex];
			int offset = mBufferOffset;

			while (bufferIndex < mBuffer.length) {

				if (mBuffer[bufferIndex].equals("") && y == rowH / 2) {
					bufferIndex++;
					continue;
				}

				if (content == null) {
					content = mBuffer[bufferIndex];
					offset = 0;
				}

				int tmp = mPaint.breakText(content.substring(offset), true,
						getDrawW(), null);

				canvas.drawText(content.substring(offset, offset + tmp), left,
						y, mPaint);

				offset += tmp;

				y += rowH;

				if (y >= mHight) {
					break;
				}

				if (offset >= content.length()) {
					offset = 0;
					content = null;
					bufferIndex++;
				}

			}

		}
	}

	boolean isDoubleByte = false;

	public TextPage(Context context, String text, int w, int h, int fontSize,
			int top, int buttom, int left, int right, int x, int y) {
		Log.d("TextPage", "w=" + w + " h=" + h);
		long prev = System.currentTimeMillis();
		mWidth = w;
		mHight = h;
		mContext = context;
		creatPaint(fontSize);
		this.top = top;
		this.buttom = buttom;
		this.left = left;
		this.right = right;
		this.xOffset = x;
		this.yOffset = y;
		Log.d("TextPage", "�ж��Ƿ�������Ⱦ");
		isDoubleByte = isRenderRow(text);

		Log.d("TextPage", "isDoubleByte" + isDoubleByte);
		mPagas = new ArrayList<Page>();
		mBuffer = text.split("\n");
		paging();
		Log.d("TextPage", "��ʼ����ʱ" + (System.currentTimeMillis() - prev));
		System.gc();

	}

	public boolean isRenderRow(String text) {
		int index = 0;
		text = text.replaceAll("\n", " ");

		String[] maxContent = text.split(" ");

		if (maxContent.length <= 1) {
			index = 0;
		} else {

			for (int i = 0; i < maxContent.length - 1; i++) {
				if (maxContent[i].length() < maxContent[i + 1].length()) {
					index = i + 1;
				}

			}
		}

		return mPaint.measureText(maxContent[index]) >= MultipleScreenTool
				.dpTopx(90, mContext);

	}

	public List<Page> getPages() {
		return mPagas;
	}

	public void paging() {

		if (isDoubleByte) {
			pagingRowPage();
		} else {
			pagingWords();
		}

	}

	public void pagingWords() {

		@SuppressWarnings("unused")
		float x = 0;
		float rowH = getRowH();
		float y = rowH / 2 + top;
		String content[] = null;
		int bufferIndex = 0;
		int offset = 0;
		Page currentPage = null;

		final float w = getDrawW();

		float h = getDrawH();

		while (bufferIndex < mBuffer.length) {

			if (mBuffer[bufferIndex].equals("") && y == rowH / 2) {
				bufferIndex++;
				continue;
			}

			if (content == null) {
				content = mBuffer[bufferIndex].split(" ");
				offset = 0;
			}

			if (currentPage == null) {

				currentPage = new Page(bufferIndex, offset);
				mPagas.add(currentPage);

			}

			float rowWidth = 0;
			for (; offset < content.length; offset++) {

				float wordsWidth = mPaint.measureText(content[offset]);
				rowWidth += wordsWidth;

				if (rowWidth > w) {
					break;
				}
				rowWidth += xOffset;

			}

			y += rowH;
			x = 0;

			if (offset >= content.length) {
				content = null;
				bufferIndex++;
				offset = 0;
			}

			if (y >= h) {
				y = rowH / 2 + top;
				currentPage = null;
			}
		}
		mYend = y;

	}

	float mYend = 0;

	public void pagingRowPage() {

		float rowH = getRowH();
		float y = rowH / 2 + top;
		String content = null;
		int bufferIndex = 0;
		int offset = 0;
		Page currentPage = null;

		final float w = getDrawW();

		float h = getDrawH();

		while (bufferIndex < mBuffer.length) {

			if (mBuffer[bufferIndex].equals("") && y == rowH / 2) {
				bufferIndex++;
				continue;
			}

			if (content == null) {
				content = mBuffer[bufferIndex];
				offset = 0;
			}

			if (currentPage == null) {
				currentPage = new RowPage(bufferIndex, offset);
				mPagas.add(currentPage);
			}

			offset += mPaint
					.breakText(content.substring(offset), true, w, null);

			y += rowH;

			if (offset >= content.length()) {
				content = null;
				bufferIndex++;
				offset = 0;
			}

			if (y >= h) {
				y = (rowH / 2 + top);
				currentPage = null;
			}
		}
		mYend = y;
	}

	private float getDrawW() {
		return (mWidth - left - right);
	}

	private float getDrawH() {
		return (mHight - top - buttom);
	}

	private void creatPaint(int size) {

		mPaint = new Paint();
		mPaint.setTextSize(MultipleScreenTool.dpTopx(size, mContext));
		mPaint.setColor(0xff858585);
		mPaint.setAntiAlias(true);
	}

	private float getRowH() {

		FontMetrics fm = mPaint.getFontMetrics();
		return (float) fm.descent - fm.top + yOffset;
	}

}
